<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>bidiformatter.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_i18n_bidiformatter.js.html">bidiformatter.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2009 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utility for formatting text for display in a potentially
<a name="line17"></a> * opposite-directionality context without garbling.
<a name="line18"></a> * Mostly a port of http://go/formatter.cc
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.i18n.BidiFormatter&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.i18n.bidi&#39;);
<a name="line25"></a>goog.require(&#39;goog.string&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Utility class for formatting text for display in a potentially
<a name="line31"></a> * opposite-directionality context without garbling. Provides the following
<a name="line32"></a> * functionality:
<a name="line33"></a> *
<a name="line34"></a> * 1. BiDi Wrapping
<a name="line35"></a> * When text in one language is mixed into a document in another, opposite-
<a name="line36"></a> * directionality language, e.g. when an English business name is embedded in a
<a name="line37"></a> * Hebrew web page, both the inserted string and the text following it may be
<a name="line38"></a> * displayed incorrectly unless the inserted string is explicitly separated
<a name="line39"></a> * from the surrounding text in a &quot;wrapper&quot; that declares its directionality at
<a name="line40"></a> * the start and then resets it back at the end. This wrapping can be done in
<a name="line41"></a> * HTML mark-up (e.g. a &#39;span dir=rtl&#39; tag) or - only in contexts where mark-up
<a name="line42"></a> * can not be used - in Unicode BiDi formatting codes (LRE|RLE and PDF).
<a name="line43"></a> * Providing such wrapping services is the basic purpose of the BiDi formatter.
<a name="line44"></a> *
<a name="line45"></a> * 2. Directionality estimation
<a name="line46"></a> * How does one know whether a string about to be inserted into surrounding
<a name="line47"></a> * text has the same directionality? Well, in many cases, one knows that this
<a name="line48"></a> * must be the case when writing the code doing the insertion, e.g. when a
<a name="line49"></a> * localized message is inserted into a localized page. In such cases there is
<a name="line50"></a> * no need to involve the BiDi formatter at all. In the remaining cases, e.g.
<a name="line51"></a> * when the string is user-entered or comes from a database, the language of
<a name="line52"></a> * the string (and thus its directionality) is not known a priori, and must be
<a name="line53"></a> * estimated at run-time. The BiDi formatter does this automatically.
<a name="line54"></a> *
<a name="line55"></a> * 3. Escaping
<a name="line56"></a> * When wrapping plain text - i.e. text that is not already HTML or HTML-
<a name="line57"></a> * escaped - in HTML mark-up, the text must first be HTML-escaped to prevent XSS
<a name="line58"></a> * attacks and other nasty business. This of course is always true, but the
<a name="line59"></a> * escaping can not be done after the string has already been wrapped in
<a name="line60"></a> * mark-up, so the BiDi formatter also serves as a last chance and includes
<a name="line61"></a> * escaping services.
<a name="line62"></a> *
<a name="line63"></a> * Thus, in a single call, the formatter will escape the input string as
<a name="line64"></a> * specified, determine its directionality, and wrap it as necessary. It is
<a name="line65"></a> * then up to the caller to insert the return value in the output.
<a name="line66"></a> *
<a name="line67"></a> * See http://wiki/Main/TemplatesAndBiDi for more information.
<a name="line68"></a> *
<a name="line69"></a> * @param {goog.i18n.bidi.Dir|number|boolean} contextDir The context
<a name="line70"></a> *     directionality. May be supplied either as a goog.i18n.bidi.Dir constant,
<a name="line71"></a> *     as a number (positive = LRT, negative = RTL, 0 = unknown) or as a boolean
<a name="line72"></a> *     (true = RTL, false = LTR).
<a name="line73"></a> * @param {boolean} opt_alwaysSpan Whether {@link #spanWrap} should always use a
<a name="line74"></a> *     &#39;span&#39; tag, even when the input directionality is neutral or matches the
<a name="line75"></a> *     context, so that the DOM structure of the output does not depend on the
<a name="line76"></a> *     combination of directionalities. Default: false.
<a name="line77"></a> * @constructor
<a name="line78"></a> */
<a name="line79"></a>goog.i18n.BidiFormatter = function(contextDir, opt_alwaysSpan) {
<a name="line80"></a>  /**
<a name="line81"></a>   * The overall directionality of the context in which the formatter is being
<a name="line82"></a>   * used.
<a name="line83"></a>   * @type {goog.i18n.bidi.Dir}
<a name="line84"></a>   * @private
<a name="line85"></a>   */
<a name="line86"></a>  this.contextDir_ = goog.i18n.bidi.toDir(contextDir);
<a name="line87"></a>
<a name="line88"></a>  /**
<a name="line89"></a>   * Whether {@link #spanWrap} and similar methods should always use the same
<a name="line90"></a>   * span structure, regardless of the combination of directionalities, for a
<a name="line91"></a>   * stable DOM structure.
<a name="line92"></a>   * @type {boolean}
<a name="line93"></a>   * @private
<a name="line94"></a>   */
<a name="line95"></a>  this.alwaysSpan_ = !!opt_alwaysSpan;
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * @return {goog.i18n.bidi.Dir} The context directionality.
<a name="line101"></a> */
<a name="line102"></a>goog.i18n.BidiFormatter.prototype.getContextDir = function() {
<a name="line103"></a>  return this.contextDir_;
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * @return {boolean} Whether alwaysSpan is set.
<a name="line109"></a> */
<a name="line110"></a>goog.i18n.BidiFormatter.prototype.getAlwaysSpan = function() {
<a name="line111"></a>  return this.alwaysSpan_;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * @param {goog.i18n.bidi.Dir|number|boolean} contextDir The context
<a name="line117"></a> *     directionality. May be supplied either as a goog.i18n.bidi.Dir constant,
<a name="line118"></a> *     as a number (positive = LRT, negative = RTL, 0 = unknown) or as a boolean
<a name="line119"></a> *     (true = RTL, false = LTR).
<a name="line120"></a> */
<a name="line121"></a>goog.i18n.BidiFormatter.prototype.setContextDir = function(contextDir) {
<a name="line122"></a>  this.contextDir_ = goog.i18n.bidi.toDir(contextDir);
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * @param {boolean} alwaysSpan Whether {@link #spanWrap} should always use a
<a name="line128"></a> *     &#39;span&#39; tag, even when the input directionality is neutral or matches the
<a name="line129"></a> *     context, so that the DOM structure of the output does not depend on the
<a name="line130"></a> *     combination of directionalities.
<a name="line131"></a> */
<a name="line132"></a>goog.i18n.BidiFormatter.prototype.setAlwaysSpan = function(alwaysSpan) {
<a name="line133"></a>  this.alwaysSpan_ = alwaysSpan;
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Returns the directionality of input argument {@code str}.
<a name="line139"></a> * Identical to {@link goog.i18n.bidi.estimateDirection}.
<a name="line140"></a> *
<a name="line141"></a> * @param {string} str The input text.
<a name="line142"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line143"></a> *     Default: false.
<a name="line144"></a> * @return {goog.i18n.bidi.Dir} Estimated overall directionality of {@code str}.
<a name="line145"></a> */
<a name="line146"></a>goog.i18n.BidiFormatter.prototype.estimateDirection =
<a name="line147"></a>    goog.i18n.bidi.estimateDirection;
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Returns true if two given directionalities are opposite.
<a name="line152"></a> * Note: the implementation is based on the numeric values of the Dir enum.
<a name="line153"></a> *
<a name="line154"></a> * @param {goog.i18n.bidi.Dir} dir1 1st directionality.
<a name="line155"></a> * @param {goog.i18n.bidi.Dir} dir2 2nd directionality.
<a name="line156"></a> * @return {boolean} Whether the directionalities are opposite.
<a name="line157"></a> * @private
<a name="line158"></a> */
<a name="line159"></a>goog.i18n.BidiFormatter.prototype.areDirectionalitiesOpposite_ = function(dir1,
<a name="line160"></a>    dir2) {
<a name="line161"></a>  return dir1 * dir2 &lt; 0;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Returns a unicode BiDi mark matching the context directionality (LRM or
<a name="line167"></a> * RLM) if {@code opt_dirReset}, and if either the directionality or the exit
<a name="line168"></a> * directionality of {@code str} is opposite to the context directionality.
<a name="line169"></a> * Otherwise returns the empty string.
<a name="line170"></a> *
<a name="line171"></a> * @param {string} str The input text.
<a name="line172"></a> * @param {goog.i18n.bidi.Dir} dir {@code str}&#39;s overall directionality.
<a name="line173"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line174"></a> *     Default: false.
<a name="line175"></a> * @param {boolean} opt_dirReset Whether to perform the reset. Default: false.
<a name="line176"></a> * @return {string} A unicode BiDi mark or the empty string.
<a name="line177"></a> * @private
<a name="line178"></a> */
<a name="line179"></a>goog.i18n.BidiFormatter.prototype.dirResetIfNeeded_ = function(str, dir,
<a name="line180"></a>    opt_isHtml, opt_dirReset) {
<a name="line181"></a>  // endsWithRtl and endsWithLtr are called only if needed (short-circuit).
<a name="line182"></a>  if (opt_dirReset &amp;&amp;
<a name="line183"></a>      (this.areDirectionalitiesOpposite_(dir, this.contextDir_) ||
<a name="line184"></a>       (this.contextDir_ == goog.i18n.bidi.Dir.LTR &amp;&amp;
<a name="line185"></a>        goog.i18n.bidi.endsWithRtl(str, opt_isHtml)) ||
<a name="line186"></a>       (this.contextDir_ == goog.i18n.bidi.Dir.RTL &amp;&amp;
<a name="line187"></a>        goog.i18n.bidi.endsWithLtr(str, opt_isHtml)))) {
<a name="line188"></a>   return this.contextDir_ == goog.i18n.bidi.Dir.LTR ?
<a name="line189"></a>       goog.i18n.bidi.Format.LRM : goog.i18n.bidi.Format.RLM;
<a name="line190"></a>  } else {
<a name="line191"></a>    return &#39;&#39;;
<a name="line192"></a>  }
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Returns &quot;rtl&quot; if {@code str}&#39;s estimated directionality is RTL, and &quot;ltr&quot; if
<a name="line198"></a> * it is LTR. In case it&#39;s UNKNOWN, returns &quot;rtl&quot; if the context directionality
<a name="line199"></a> * is RTL, and &quot;ltr&quot; otherwise.
<a name="line200"></a> * Needed for GXP, which can&#39;t handle dirAttr.
<a name="line201"></a> * Example use case:
<a name="line202"></a> * &lt;td expr:dir=&#39;bidiFormatter.dirAttrValue(foo)&#39;&gt;&lt;gxp:eval expr=&#39;foo&#39;&gt;&lt;/td&gt;
<a name="line203"></a> *
<a name="line204"></a> * @param {string} str Text whose directionality is to be estimated.
<a name="line205"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line206"></a> *     Default: false.
<a name="line207"></a> * @return {string} &quot;rtl&quot; or &quot;ltr&quot;, according to the logic described above.
<a name="line208"></a> */
<a name="line209"></a>goog.i18n.BidiFormatter.prototype.dirAttrValue = function(str, opt_isHtml) {
<a name="line210"></a>  return this.knownDirAttrValue(this.estimateDirection(str, opt_isHtml));
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Returns &quot;rtl&quot; if the given directionality is RTL, and &quot;ltr&quot; if it is LTR. In
<a name="line216"></a> * case it&#39;s UNKNOWN, returns &quot;rtl&quot; if the context directionality is RTL, and
<a name="line217"></a> * &quot;ltr&quot; otherwise.
<a name="line218"></a> *
<a name="line219"></a> * @param {goog.i18n.bidi.Dir} dir A directionality.
<a name="line220"></a> * @return {string} &quot;rtl&quot; or &quot;ltr&quot;, according to the logic described above.
<a name="line221"></a> */
<a name="line222"></a>goog.i18n.BidiFormatter.prototype.knownDirAttrValue = function(dir) {
<a name="line223"></a>  if (dir == goog.i18n.bidi.Dir.UNKNOWN) {
<a name="line224"></a>    dir = this.contextDir_;
<a name="line225"></a>  }
<a name="line226"></a>
<a name="line227"></a>  return dir == goog.i18n.bidi.Dir.RTL ? &#39;rtl&#39; : &#39;ltr&#39;;
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Returns &quot;dir=ltr&quot; or &quot;dir=rtl&quot;, depending on {@code str}&#39;s estimated
<a name="line233"></a> * directionality, if it is not the same as the context directionality.
<a name="line234"></a> * Otherwise, returns the empty string.
<a name="line235"></a> *
<a name="line236"></a> * @param {string} str Text whose directionality is to be estimated.
<a name="line237"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line238"></a> *     Default: false.
<a name="line239"></a> * @return {string} &quot;dir=rtl&quot; for RTL text in non-RTL context; &quot;dir=ltr&quot; for LTR
<a name="line240"></a> *     text in non-LTR context; else, the empty string.
<a name="line241"></a> */
<a name="line242"></a>goog.i18n.BidiFormatter.prototype.dirAttr = function(str, opt_isHtml) {
<a name="line243"></a>  return this.knownDirAttr(this.estimateDirection(str, opt_isHtml));
<a name="line244"></a>};
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>/**
<a name="line248"></a> * Returns &quot;dir=ltr&quot; or &quot;dir=rtl&quot;, depending on the given directionality, if it
<a name="line249"></a> * is not the same as the context directionality. Otherwise, returns the empty
<a name="line250"></a> * string.
<a name="line251"></a> *
<a name="line252"></a> * @param {goog.i18n.bidi.Dir} dir A directionality.
<a name="line253"></a> * @return {string} &quot;dir=rtl&quot; for RTL text in non-RTL context; &quot;dir=ltr&quot; for LTR
<a name="line254"></a> *     text in non-LTR context; else, the empty string.
<a name="line255"></a> */
<a name="line256"></a>goog.i18n.BidiFormatter.prototype.knownDirAttr = function(dir) {
<a name="line257"></a>  if (dir != this.contextDir_) {
<a name="line258"></a>    return dir == goog.i18n.bidi.Dir.RTL ? &#39;dir=rtl&#39; :
<a name="line259"></a>        dir == goog.i18n.bidi.Dir.LTR ? &#39;dir=ltr&#39; : &#39;&#39;;
<a name="line260"></a>  }
<a name="line261"></a>  return &#39;&#39;;
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Formats a string of unknown directionality for use in HTML output of the
<a name="line267"></a> * context directionality, so an opposite-directionality string is neither
<a name="line268"></a> * garbled nor garbles what follows it.
<a name="line269"></a> * The algorithm: estimates the directionality of input argument {@code str}. In
<a name="line270"></a> * case its directionality doesn&#39;t match the context directionality, wraps it
<a name="line271"></a> * with a &#39;span&#39; tag and adds a &quot;dir&quot; attribute (either &#39;dir=rtl&#39; or &#39;dir=ltr&#39;).
<a name="line272"></a> * If setAlwaysSpan(true) was used, the input is always wrapped with &#39;span&#39;,
<a name="line273"></a> * skipping just the dir attribute when it&#39;s not needed.
<a name="line274"></a> *
<a name="line275"></a> * If {@code opt_dirReset}, and if the overall directionality or the exit
<a name="line276"></a> * directionality of {@code str} are opposite to the context directionality, a
<a name="line277"></a> * trailing unicode BiDi mark matching the context directionality is appened
<a name="line278"></a> * (LRM or RLM).
<a name="line279"></a> *
<a name="line280"></a> * If !{@code opt_isHtml}, HTML-escapes {@code str} regardless of wrapping.
<a name="line281"></a> *
<a name="line282"></a> * @param {string} str The input text.
<a name="line283"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line284"></a> *     Default: false.
<a name="line285"></a> * @param {boolean} opt_dirReset Whether to append a trailing unicode bidi mark
<a name="line286"></a> *     matching the context directionality, when needed, to prevent the possible
<a name="line287"></a> *     garbling of whatever may follow {@code str}. Default: true.
<a name="line288"></a> * @return {string} Input text after applying the above processing.
<a name="line289"></a> */
<a name="line290"></a>goog.i18n.BidiFormatter.prototype.spanWrap = function(str, opt_isHtml,
<a name="line291"></a>    opt_dirReset) {
<a name="line292"></a>  var dir = this.estimateDirection(str, opt_isHtml);
<a name="line293"></a>  return this.spanWrapWithKnownDir(dir, str, opt_isHtml, opt_dirReset);
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Formats a string of given directionality for use in HTML output of the
<a name="line299"></a> * context directionality, so an opposite-directionality string is neither
<a name="line300"></a> * garbled nor garbles what follows it.
<a name="line301"></a> * The algorithm: If {@code dir} doesn&#39;t match the context directionality, wraps
<a name="line302"></a> * {@code str} with a &#39;span&#39; tag and adds a &quot;dir&quot; attribute (either &#39;dir=rtl&#39; or
<a name="line303"></a> * &#39;dir=ltr&#39;). If setAlwaysSpan(true) was used, the input is always wrapped with
<a name="line304"></a> * &#39;span&#39;, skipping just the dir attribute when it&#39;s not needed.
<a name="line305"></a> *
<a name="line306"></a> * If {@code opt_dirReset}, and if {@code dir} or the exit directionality of
<a name="line307"></a> * {@code str} are opposite to the context directionality, a trailing unicode
<a name="line308"></a> * BiDi mark matching the context directionality is appened (LRM or RLM).
<a name="line309"></a> *
<a name="line310"></a> * If !{@code opt_isHtml}, HTML-escapes {@code str} regardless of wrapping.
<a name="line311"></a> *
<a name="line312"></a> * @param {goog.i18n.bidi.Dir} dir {@code str}&#39;s overall directionality.
<a name="line313"></a> * @param {string} str The input text.
<a name="line314"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line315"></a> *     Default: false.
<a name="line316"></a> * @param {boolean} opt_dirReset Whether to append a trailing unicode bidi mark
<a name="line317"></a> *     matching the context directionality, when needed, to prevent the possible
<a name="line318"></a> *     garbling of whatever may follow {@code str}. Default: true.
<a name="line319"></a> * @return {string} Input text after applying the above processing.
<a name="line320"></a> */
<a name="line321"></a>goog.i18n.BidiFormatter.prototype.spanWrapWithKnownDir = function(dir, str,
<a name="line322"></a>    opt_isHtml, opt_dirReset) {
<a name="line323"></a>  opt_dirReset = opt_dirReset || (opt_dirReset == undefined);
<a name="line324"></a>  // Whether to add the &quot;dir&quot; attribute.
<a name="line325"></a>  var dirCondition = dir != goog.i18n.bidi.Dir.UNKNOWN &amp;&amp; dir !=
<a name="line326"></a>      this.contextDir_;
<a name="line327"></a>  if (!opt_isHtml) {
<a name="line328"></a>    str = goog.string.htmlEscape(str);
<a name="line329"></a>  }
<a name="line330"></a>
<a name="line331"></a>  var result = [];
<a name="line332"></a>  if (this.alwaysSpan_ || dirCondition) {  // Wrap is needed
<a name="line333"></a>    result.push(&#39;&lt;span&#39;);
<a name="line334"></a>    if (dirCondition) {
<a name="line335"></a>      result.push(dir == goog.i18n.bidi.Dir.RTL ? &#39; dir=rtl&#39; : &#39; dir=ltr&#39;);
<a name="line336"></a>    }
<a name="line337"></a>    result.push(&#39;&gt;&#39; + str + &#39;&lt;/span&gt;&#39;);
<a name="line338"></a>  } else {
<a name="line339"></a>    result.push(str);
<a name="line340"></a>  }
<a name="line341"></a>
<a name="line342"></a>  result.push(this.dirResetIfNeeded_(str, dir, true, opt_dirReset));
<a name="line343"></a>  return result.join(&#39;&#39;);
<a name="line344"></a>};
<a name="line345"></a>
<a name="line346"></a>
<a name="line347"></a>/**
<a name="line348"></a> * Formats a string of unknown directionality for use in plain-text output of
<a name="line349"></a> * the context directionality, so an opposite-directionality string is neither
<a name="line350"></a> * garbled nor garbles what follows it.
<a name="line351"></a> * As opposed to {@link #spanWrap}, this makes use of unicode BiDi formatting
<a name="line352"></a> * characters. In HTML, its *only* valid use is inside of elements that do not
<a name="line353"></a> * allow mark-up, e.g. an &#39;option&#39; tag.
<a name="line354"></a> * The algorithm: estimates the directionality of input argument {@code str}.
<a name="line355"></a> * In case it doesn&#39;t match  the context directionality, wraps it with Unicode
<a name="line356"></a> * BiDi formatting characters: RLE{@code str}PDF for RTL text, and
<a name="line357"></a> * LRE{@code str}PDF for LTR text.
<a name="line358"></a> *
<a name="line359"></a> * If {@code opt_dirReset}, and if the overall directionality or the exit
<a name="line360"></a> * directionality of {@code str} are opposite to the context directionality, a
<a name="line361"></a> * trailing unicode BiDi mark matching the context directionality is appended
<a name="line362"></a> * (LRM or RLM).
<a name="line363"></a> *
<a name="line364"></a> * Does *not* do HTML-escaping regardless of the value of {@code opt_isHtml}.
<a name="line365"></a> * The return value can be HTML-escaped as necessary.
<a name="line366"></a> *
<a name="line367"></a> * @param {string} str The input text.
<a name="line368"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line369"></a> *     Default: false.
<a name="line370"></a> * @param {boolean} opt_dirReset Whether to append a trailing unicode bidi mark
<a name="line371"></a> *     matching the context directionality, when needed, to prevent the possible
<a name="line372"></a> *     garbling of whatever may follow {@code str}. Default: true.
<a name="line373"></a> * @return {string} Input text after applying the above processing.
<a name="line374"></a> */
<a name="line375"></a>goog.i18n.BidiFormatter.prototype.unicodeWrap = function(str, opt_isHtml,
<a name="line376"></a>    opt_dirReset) {
<a name="line377"></a>  var dir = this.estimateDirection(str, opt_isHtml);
<a name="line378"></a>  return this.unicodeWrapWithKnownDir(dir, str, opt_isHtml, opt_dirReset);
<a name="line379"></a>};
<a name="line380"></a>
<a name="line381"></a>
<a name="line382"></a>/**
<a name="line383"></a> * Formats a string of given directionality for use in plain-text output of the
<a name="line384"></a> * context directionality, so an opposite-directionality string is neither
<a name="line385"></a> * garbled nor garbles what follows it.
<a name="line386"></a> * As opposed to {@link #spanWrapWithKnownDir}, makes use of unicode BiDi
<a name="line387"></a> * formatting characters. In HTML, its *only* valid use is inside of elements
<a name="line388"></a> * that do not allow mark-up, e.g. an &#39;option&#39; tag.
<a name="line389"></a> * The algorithm: If {@code dir} doesn&#39;t match the context directionality, wraps
<a name="line390"></a> * {@code str} with Unicode BiDi formatting characters: RLE{@code str}PDF for
<a name="line391"></a> * RTL text, and LRE{@code str}PDF for LTR text.
<a name="line392"></a> *
<a name="line393"></a> * If {@code opt_dirReset}, and if the overall directionality or the exit
<a name="line394"></a> * directionality of {@code str} are opposite to the context directionality, a
<a name="line395"></a> * trailing unicode BiDi mark matching the context directionality is appended
<a name="line396"></a> * (LRM or RLM).
<a name="line397"></a> *
<a name="line398"></a> * Does *not* do HTML-escaping regardless of the value of {@code opt_isHtml}.
<a name="line399"></a> * The return value can be HTML-escaped as necessary.
<a name="line400"></a> *
<a name="line401"></a> * @param {goog.i18n.bidi.Dir} dir {@code str}&#39;s overall directionality.
<a name="line402"></a> * @param {string} str The input text.
<a name="line403"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line404"></a> *     Default: false.
<a name="line405"></a> * @param {boolean} opt_dirReset Whether to append a trailing unicode bidi mark
<a name="line406"></a> *     matching the context directionality, when needed, to prevent the possible
<a name="line407"></a> *     garbling of whatever may follow {@code str}. Default: true.
<a name="line408"></a> * @return {string} Input text after applying the above processing.
<a name="line409"></a> */
<a name="line410"></a>goog.i18n.BidiFormatter.prototype.unicodeWrapWithKnownDir = function(dir, str,
<a name="line411"></a>    opt_isHtml, opt_dirReset) {
<a name="line412"></a>  opt_dirReset = opt_dirReset || (opt_dirReset == undefined);
<a name="line413"></a>  var result = [];
<a name="line414"></a>  if (dir != goog.i18n.bidi.Dir.UNKNOWN &amp;&amp; dir != this.contextDir_) {
<a name="line415"></a>    result.push(dir == goog.i18n.bidi.Dir.RTL ? goog.i18n.bidi.Format.RLE :
<a name="line416"></a>                                                goog.i18n.bidi.Format.LRE);
<a name="line417"></a>    result.push(str);
<a name="line418"></a>    result.push(goog.i18n.bidi.Format.PDF);
<a name="line419"></a>  } else {
<a name="line420"></a>    result.push(str);
<a name="line421"></a>  }
<a name="line422"></a>
<a name="line423"></a>  result.push(this.dirResetIfNeeded_(str, dir, opt_isHtml, opt_dirReset));
<a name="line424"></a>  return result.join(&#39;&#39;);
<a name="line425"></a>};
<a name="line426"></a>
<a name="line427"></a>
<a name="line428"></a>/**
<a name="line429"></a> * Returns a Unicode BiDi mark matching the context directionality (LRM or RLM)
<a name="line430"></a> * if the directionality or the exit directionality of {@code str} are opposite
<a name="line431"></a> * to the context directionality. Otherwise returns the empty string.
<a name="line432"></a> *
<a name="line433"></a> * @param {string} str The input text.
<a name="line434"></a> * @param {boolean} opt_isHtml Whether {@code str} is HTML / HTML-escaped.
<a name="line435"></a> *     Default: false.
<a name="line436"></a> * @return {string} A Unicode bidi mark matching the global directionality or
<a name="line437"></a> *     the empty string.
<a name="line438"></a> */
<a name="line439"></a>goog.i18n.BidiFormatter.prototype.markAfter = function(str, opt_isHtml) {
<a name="line440"></a>  return this.dirResetIfNeeded_(str,
<a name="line441"></a>      this.estimateDirection(str, opt_isHtml), opt_isHtml, true);
<a name="line442"></a>};
<a name="line443"></a>
<a name="line444"></a>
<a name="line445"></a>/**
<a name="line446"></a> * Returns the Unicode BiDi mark matching the context directionality (LRM for
<a name="line447"></a> * LTR context directionality, RLM for RTL context directionality), or the
<a name="line448"></a> * empty string for neutral / unknown context directionality.
<a name="line449"></a> *
<a name="line450"></a> * @return {string} LRM for LTR context directionality and RLM for RTL context
<a name="line451"></a> *     directionality.
<a name="line452"></a> */
<a name="line453"></a>goog.i18n.BidiFormatter.prototype.mark = function() {
<a name="line454"></a>  switch (this.contextDir_) {
<a name="line455"></a>  case (goog.i18n.bidi.Dir.LTR):
<a name="line456"></a>      return goog.i18n.bidi.Format.LRM;
<a name="line457"></a>  case (goog.i18n.bidi.Dir.RTL):
<a name="line458"></a>      return goog.i18n.bidi.Format.RLM;
<a name="line459"></a>  default:
<a name="line460"></a>      return &#39;&#39;;
<a name="line461"></a>  }
<a name="line462"></a>};
<a name="line463"></a>
<a name="line464"></a>
<a name="line465"></a>/**
<a name="line466"></a> * Returns &#39;right&#39; for RTL context directionality. Otherwise (LTR or neutral /
<a name="line467"></a> * unknown context directionality) returns &#39;left&#39;.
<a name="line468"></a> *
<a name="line469"></a> * @return {string} &#39;right&#39; for RTL context directionality and &#39;left&#39; for other
<a name="line470"></a> *     context directionality.
<a name="line471"></a> */
<a name="line472"></a>goog.i18n.BidiFormatter.prototype.startEdge = function() {
<a name="line473"></a>  return this.contextDir_ == goog.i18n.bidi.Dir.RTL ?
<a name="line474"></a>      goog.i18n.bidi.RIGHT : goog.i18n.bidi.LEFT;
<a name="line475"></a>};
<a name="line476"></a>
<a name="line477"></a>
<a name="line478"></a>/**
<a name="line479"></a> * Returns &#39;left&#39; for RTL context directionality. Otherwise (LTR or neutral /
<a name="line480"></a> * unknown context directionality) returns &#39;right&#39;.
<a name="line481"></a> *
<a name="line482"></a> * @return {string} &#39;left&#39; for RTL context directionality and &#39;right&#39; for other
<a name="line483"></a> *     context directionality.
<a name="line484"></a> */
<a name="line485"></a>goog.i18n.BidiFormatter.prototype.endEdge = function() {
<a name="line486"></a>  return this.contextDir_ == goog.i18n.bidi.Dir.RTL ?
<a name="line487"></a>      goog.i18n.bidi.LEFT : goog.i18n.bidi.RIGHT;
<a name="line488"></a>};
</pre>


</body>
</html>
